<?php
namespace Home\Controller;
use Think\Controller;
class OrderController extends Controller {
	public function _initialize()
    {
        //未登录、登录超时、长时间未动
    	if (empty($_SESSION['uid']) || time() > $_SESSION['overtime'] || empty($_SESSION['overtime']) ) {

    		$this->redirect('Common/login');
    	}

        session('overtime',(time()+60*60*C('overtime')));
    }


    /**
     * [selllist 挂卖报表]
     * @return [type] [description]
     */
    public function selllist(){
    	$uid = $_SESSION['uid'];
    	$m_order = M('Order');
        $m_user = M('User');

    	$where_order['sell_uid'] = $uid;
    	$where_order['status'] = array('egt',1);

    	$count = $m_order->where($where_order)->count();
        $Page  = new \Think\Page($count,20);
        $show  = $Page->show();

        $sellList = $m_order->where($where_order)->order('id desc')->limit($Page->firstRow.','.$Page->listRows)->select();
        foreach ($sellList as $key => $value) {
            $where_user['id'] = $value['buy_uid'];
            $buyphone = $m_user->where($where_user)->getfield('phone');
            $sellList[$key]['buyphone'] = $buyphone;

        }

        $this->assign('page',$show);
    	$this->assign('sellList',$sellList);
    	$this->display();
    }


    /**
     * [ctrl_finish 确认]
     * @return [type] [description]
     */
    public function ctrl_finish(){
    	if(IS_AJAX){
    		$id = $_REQUEST['id'];

    		$m_order = M("Order");
    		$m_user = M('User');

    		$where_order['id'] = $id;
    		$orderInfo = $m_order->where($where_order)->find();

    		if ($orderInfo['status'] == 3) {
    			$this->ajaxReturn(array(
                    'code'=>0,
                    'msg' => '订单已完成，请勿重复操作'
                ));
    		}else{
    			$save_order['status'] = 3;
    			$save_order['finishtime'] = time();

    			$res = $m_order->where($where_order)->save($save_order);
    			if ($res) {
    				// 买家修改中心积分、添加中心积分记录
    				$where_user['id'] = $orderInfo['buy_uid'];
    				$userInfo = $m_user->where($where_user)->find();

    				$old_center = $userInfo['score_center'];
    				$change_center = $orderInfo['num'];
    				$new_center = $old_center + $change_center;

    				$save_user['score_center'] = $new_center;
	        		$result = $m_user->where($where_user)->save($save_user);
	        		if ($result) {
	        			//添加中心积分记录
	        			addCenterTable($userInfo['id'],$old_center,$change_center,$new_center,'购买积分，新增中心积分'.$change_center);
	        		}

    				$this->ajaxReturn(array(
	                    'code'=>1,
	                    'msg' => '操作成功'
	                ));
    			}else{
    				$this->ajaxReturn(array(
	                    'code'=>0,
	                    'msg' => '操作失败'
	                ));
    			}
    		}
    	}else{
    		$this->redirect('Common/BadRequest');
    	}
    }


    /**
     * [show 查看凭证]
     * @return [type] [description]
     */
    public function show(){
    	$id = $_REQUEST['id'];

    	$m_order = M('Order');

    	$where_order['id'] = $id;
    	$pic_url = $m_order->where($where_order)->getfield('pic_url');

    	
    	$this->assign('pic_url',$pic_url);
    	$this->display();
    }

    /**
     * [buylist 购买报表]
     * @return [type] [description]
     */
    public function buylist(){
    	$uid = $_SESSION['uid'];
    	$m_order = M('Order');
        $m_user = M('User');

    	$where_order['buy_uid'] = $uid;
    	$where_order['status'] = array('egt',1);

    	$count = $m_order->where($where_order)->count();
        $Page  = new \Think\Page($count,20);
        $show  = $Page->show();

        $buyList = $m_order->where($where_order)->order('id desc')->limit($Page->firstRow.','.$Page->listRows)->select();
        foreach ($buyList as $key => $value) {
            $where_user['id'] = $value['sell_uid'];
            $sellphone = $m_user->where($where_user)->getfield('phone');
            $buyList[$key]['sellphone'] = $sellphone;

        }

        $this->assign('page',$show);
    	$this->assign('buyList',$buyList);
    	$this->display();
    }

    /**
     * [order_detail 订单详情]
     * @return [type] [description]
     */
    public function orderdetail(){
    	$id = $_REQUEST['id'];

    	$m_order = M('Order');
    	$m_user = M('User');

    	$where_order['id'] = $id;
    	$orderInfo = $m_order->where($where_order)->find();

    	$where_user['id'] = $orderInfo['sell_uid'];
    	$sellInfo = $m_user->where($where_user)->find();

    	$this->assign('orderInfo',$orderInfo);
    	$this->assign('sellInfo',$sellInfo);
    	$this->display();
    }


    /**
     * [ctrl_submitPic 上传凭证]
     * @return [type] [description]
     */
    public function ctrl_submitPic(){
    	
    	if (IS_AJAX) {
    		$id = $_REQUEST['orderid'];
    		$pic_url = trim($_REQUEST['pic_url']);

    		$m_order = M('Order');

    		$where['id'] = $id;

            $order_pic_url = $m_order->where($where)->getfield('pic_url');
            if ($order_pic_url == $pic_url ) {
                $this->ajaxReturn(array(
                    'code'=>1,
                    'msg' => '提交成功'
                ));
            }else{
                $save['pic_url'] = $pic_url;
                // $save['status'] = 2;
                // $save['paytime'] = time();
                $res = $m_order->where($where)->save($save);
                if ($res) {
                    $this->ajaxReturn(array(
                        'code'=>1,
                        'msg' => '提交成功'
                    ));
                }else{
                    $this->ajaxReturn(array(
                        'code'=>0,
                        'msg' => '提交失败，请重新尝试'
                    ));
                }
            }
    	}else{
    		$this->redirect('Common/BadRequest');
    	}
    }



    public function ctrl_buyfinish(){
        if (IS_AJAX) {
            $id = $_REQUEST['id'];
            $m_order = M('Order');

            $where['id'] = $id;
            $save['status'] = 2;
            $save['paytime'] = time();

            $res = $m_order->where($where)->save($save);
            if ($res) {
                $this->ajaxReturn(array(
                    'code'=>1,
                    'msg' => '操作成功'
                ));
            }else{
                $this->ajaxReturn(array(
                    'code'=>0,
                    'msg' => '操作失败'
                ));
            }
        }else{
            $this->redirect('Common/BadRequest');
        }
    }


    /**
     * [buy 购买交易积分]
     * @return [type] [description]
     */
    public function buy(){

        $hallStart = strtotime(C('hallStart'));
        $hallEnd = strtotime(C('hallEnd'));

        $nowTime = time();

        if ($nowTime >= $hallStart && $nowTime < $hallEnd ) {

        	if (empty($_REQUEST['p'])) {
        		$p = 1;
        	}else{
        		$p = $_REQUEST['p'];
        	}

        	$uid = $_SESSION['uid'];
        	$m_order = M('Order');

        	$where_order['status'] = 0;

        	$count = $m_order->where($where_order)->count();
            $Page  = new \Think\Page($count,10);
            $show  = $Page->show();

        	$buyList = $m_order->where($where_order)->order('id asc')->limit($Page->firstRow.','.$Page->listRows)->select();

        	$this->assign('uid',$uid);
        	$this->assign('p',$p);
            $this->assign('page',$show);
        	$this->assign('buyList',$buyList);
        	$this->display();

        }else{
            $this->redirect('Common/notopen',array('type' => 11));
        }
    }


    /**
     * [ctrl_buy 购买交易积分执行函数]
     * @return [type] [description]
     */
    public function ctrl_buy(){
    	if(IS_AJAX){
            $hallStart = strtotime(C('hallStart'));
            $hallEnd = strtotime(C('hallEnd'));

            $nowTime = time();

            if ($nowTime >= $hallStart && $nowTime < $hallEnd ) {
        		$uid = $_SESSION['uid'];
        		$id = $_REQUEST['id'];

        		$m_user = M('User');
        		$m_order = M('Order');

                $where_notfinish['buy_uid'] = $uid;
                $where_notfinish['status'] = array('BETWEEN','1,2');
                $notfinishInfo = $m_order->where($where_notfinish)->order('id desc')->find();
                if ($notfinishInfo['id']) {
                    $this->ajaxReturn(array(
                        'code'=>0,
                        'msg' => '您有未完成的订单，无法继续购买'
                    ));
                }else{
                    $where_order['id'] = $id;
                    $orderInfo = $m_order->where($where_order)->find();
                    if ($orderInfo['sell_uid'] == $uid) {
                        $this->ajaxReturn(array(
                            'code'=>0,
                            'msg' => '不可购买自己挂售的'
                        ));
                    }else{
                        if ($orderInfo['status'] < 0) {
                            $this->ajaxReturn(array(
                                'code'=>0,
                                'msg' => '用户已撤回'
                            ));
                        }elseif ($orderInfo['status'] >= 1) {
                            $this->ajaxReturn(array(
                                'code'=>0,
                                'msg' => '已出售，请勿重复操作'
                            ));
                        }else{
                            $where_user['id'] = $uid;
                            $userInfo = $m_user->where($where_user)->find();

                            $save['buy_uid'] = $userInfo['id'];
                            $save['buy_account'] = $userInfo['account'];
                            $save['buytime'] = time();
                            $save['status'] = 1;

                            $res = $m_order->where($where_order)->save($save);
                            if ($res) {
                                $this->ajaxReturn(array(
                                    'code'=>1,
                                    'msg' => '购买成功'
                                ));
                            }else{
                                $this->ajaxReturn(array(
                                    'code'=>0,
                                    'msg' => '购买失败'
                                ));
                            }
                        }
                    }
                }
            }else{
                $this->ajaxReturn(array(
                    'code'=>0,
                    'msg' => '当前时间未开放，无法购买'
                ));
            }
    	}else{
    		$this->redirect('Common/BadRequest');
    	}
    }


    /**
     * [sell 挂卖交易积分]
     * @return [type] [description]
     */
    public function sell(){
    	$uid = $_SESSION['uid'];

    	$scope= array();
    	$scope['min'] = C('sellMin');
    	$scope['max'] = C('sellMax');
        
        $m_user = M('User');
        $m_order = M('Order');

        $where_user['id'] = $uid;
        $userInfo = $m_user->where($where_user)->find();

        $where_order['sell_uid'] = $uid;

        $count = $m_order->where($where_order)->count();
        $Page  = new \Think\Page($count,20);
        $show  = $Page->show();

        
        $sellList = $m_order->where($where_order)->order('id desc')->limit($Page->firstRow.','.$Page->listRows)->select();


        $this->assign('userInfo',$userInfo);
        $this->assign('scope',$scope);
        $this->assign('page',$show);
        $this->assign('sellList',$sellList);
    	$this->display();
    }

    /**
     * [ctrl_sell 挂卖执行函数]
     * @return [type] [description]
     */
    public function ctrl_sell(){
    	if(IS_AJAX){
    		$uid = $_SESSION['uid'];
    		$sell = trim($_REQUEST['sell']);

    		$m_user = M('User');
    		$m_order = M('Order');

    		$scope['min'] = C('sellMin');
    		$scope['max'] = C('sellMax');

	        $where_user['id'] = $uid;
	        $userInfo = $m_user->where($where_user)->find();
	        if ($userInfo['score_trading'] < $sell) {
	        	$this->ajaxReturn(array(
                    'code'=>0,
                    'msg' => '交易积分不足，无法挂卖'
                ));
	        }else{
	        	if ($sell < $scope['min'] || $sell > $scope['max'] || empty($sell)) {
	        		$this->ajaxReturn(array(
	                    'code'=>0,
	                    'msg' => '交易积分超出挂卖范围'
	                ));
	        	}else{
	        		//修改交易积分数据、添加交易积分记录、添加订单
	        		$old_trading = $userInfo['score_trading'];
	        		$change_trading = $sell;
	        		$new_trading = $old_trading - $change_trading;

	        		$save['score_trading'] = $new_trading;
	        		$res = $m_user->where($where_user)->save($save);
	        		if ($res) {
	        			//添加交易积分记录
	        			addTradingTable($uid,$old_trading,$change_trading,$new_trading,'挂卖交易积分扣除'.$change_trading,2);

	        			// 添加订单
	        			$add['sell_uid'] = $uid;
	        			$add['sell_account'] = $userInfo['account'];
	        			$add['num'] = $change_trading;
	        			$add['createtime'] = time();
	        			$add['status'] = 0;

	        			$result = $m_order->add($add);
	        			$this->ajaxReturn(array(
		                    'code'=>1,
		                    'msg' => '挂卖成功',
		                    'data' => $result,
		                    'date' =>date("Y-m-d H:i:s",$add['createtime'])
		                ));
	        		}else{
	        			$this->ajaxReturn(array(
		                    'code'=>0,
		                    'msg' => '挂卖失败'
		                ));
	        		}
	        	}
	        }
    	}else{
    		$this->redirect('Common/BadRequest');
    	}
    }


    /**
     * [ctrl_cancel 取消挂卖]
     * @return [type] [description]
     */
    public function ctrl_cancel(){
    	if (IS_AJAX) {
    		$id = $_REQUEST['id'];
    		$m_order = M('Order');
    		$m_user = M('User');

    		$where_order['id'] = $id;
    		$orderInfo = $m_order->where($where_order)->find();
    		if ($orderInfo['status'] == -1) {
    			$this->ajaxReturn(array(
                    'code'=>0,
                    'msg' => '已经撤回，请勿重复操作'
                ));
    		}elseif ($orderInfo['status'] >=  1) {
    			$this->ajaxReturn(array(
                    'code'=>0,
                    'msg' => '已出售，无法操作'
                ));
    		}else{
    			$where_user['id'] = $orderInfo['sell_uid'];
    			$userInfo = $m_user->where($where_user)->find();

    			//修改订单状态、修改用户数据、添加交易积分记录
    			$save_order['status'] = -1;
    			$res = $m_order->where($where_order)->save($save_order);
    			if ($res) {
    				$old_trading = $userInfo['score_trading'];
    				$change_trading = $orderInfo['num'];
    				$new_trading = $old_trading + $change_trading;

    				$save_user['score_trading'] = $new_trading;
	        		$result = $m_user->where($where_user)->save($save_user);
	        		if ($result) {
	        			// 添加交易积分记录
	        			addTradingTable($userInfo['id'],$old_trading,$change_trading,$new_trading,'撤回挂卖返回'.$change_trading);
	        		}
	        		$this->ajaxReturn(array(
	                    'code'=>1,
	                    'msg' => '撤回成功'
	                ));

    			}else{
    				$this->ajaxReturn(array(
	                    'code'=>0,
	                    'msg' => '撤回失败'
	                ));
    			}
    		}

    	}else{
    		$this->redirect('Common/BadRequest');
    	}
    }


    /**
     * [buycenter 购买中心积分]
     * @return [type] [description]
     */
    public function buycenter(){
        $uid = $_SESSION['uid'];

        $lowest = C('lowest');//小于多少条

        $m_user = M('User');
        $m_order = M('Order');
        $m_sellcenter = M('Sell_center');
        $m_ordercenter = M('Order_center');

        $where_order['status'] = 0;
        $ordercount =  $m_order->where($where_order)->count()+0;

        $where['user_id'] = $uid;
        $where['status'] = array('in','0,1');
        $countOrder = $m_ordercenter->where($where)->count()+0;

        if ($ordercount < $lowest || $countOrder > 0 ) {
            $where_user['id'] = $uid;
            $userInfo = $m_user->where($where_user)->find();

            $sellcenterList = $m_sellcenter->order('id asc')->select();

            $where_ordercenter['user_id'] = $uid;

            // $count = $m_ordercenter->where($where_ordercenter)->count();
            // $Page  = new \Think\Page($count,20);
            // $show  = $Page->show();

            // $ordercenterList = $m_ordercenter->where($where_ordercenter)->order('id desc')->limit($Page->firstRow.','.$Page->listRows)->select();
            $ordercenterList = $m_ordercenter->where($where_ordercenter)->order('id desc')->limit(5)->select();

            $this->assign('userInfo',$userInfo);
            $this->assign('sellcenterList',$sellcenterList);
            // $this->assign('page',$show);
            $this->assign('ordercenterList',$ordercenterList);
            $this->display();
        }else{
            $this->redirect('Common/notopen',array('type' => 1));
        }
    }


    /**
     * [ctrl_buyCenter 购买平台中心积分]
     * @return [type] [description]
     */
    public function ctrl_buyCenter(){
        if (IS_AJAX) {
            $id = $_REQUEST['id'];
            $uid = $_SESSION['uid'];

            $lowest = C('lowest');//小于多少条

            $m_order = M('Order');
            $m_sellcenter = M('Sell_center');
            $m_ordercenter = M('Order_center');
            $m_user = M('User');

            $where_order['status'] = 0;
            $ordercount =  $m_order->where($where_order)->count()+0;

            $where_user['id'] = $uid;
            $userStatus = $m_user->where($where_user)->getfield('status');
            if ($userStatus == 0) {
                $this->ajaxReturn(array(
                    'code'=>0,
                    'msg' => '账户未激活，无法购买'
                ));
            }elseif ($userStatus == 2) {
                $this->ajaxReturn(array(
                    'code'=>0,
                    'msg' => '账户已冻结，无法购买'
                ));
            }else{
                if ($ordercount < $lowest ) {
                    $where_sell['id'] = $id;
                    $sellInfo = $m_sellcenter->where($where_sell)->find();
                    if (empty($sellInfo['id'])) {
                        $this->ajaxReturn(array(
                            'code'=>0,
                            'msg' => '购买目标不存在，请刷新后重新操作'
                        ));
                    }else{
                        // print_r($sellInfo);die();
                        $where_ordercenter['user_id'] = $uid;
                        $where_ordercenter['status'] = array('in','0,1');
                        $ordercenterInfo = $m_ordercenter->where($where_ordercenter)->order('id desc')->find();
                        if (empty($ordercenterInfo['id'])) {

                            $now_time = time();
                            $lastorderInfo = $m_ordercenter->order('id desc')->find();

                            
                            if (empty($lastorderInfo['id'])) {
                                $add['type'] = 0;
                                $difference_time = 10;
                            }else{
                                $difference_time = $now_time - $lastorderInfo['createtime'];
                                if ($lastorderInfo['type'] >= 3) {
                                    $add['type'] = 0;
                                }else{
                                    $add['type'] = $lastorderInfo['type'] + 1;
                                }
                            }


                            if ($difference_time < 5 ) {
                                $this->ajaxReturn(array(
                                    'code'=>0,
                                    'msg' => '系统繁忙,请稍后购买'
                                ));
                            }else{
                                $walletArr = explode(',', $sellInfo['wallet_address']);
                                $phoneArr = explode(',', $sellInfo['phone']);


                                $add['user_id'] = $uid;
                                $add['num'] = $sellInfo['num'];
                                $add['discount'] = $sellInfo['discount'];
                                $add['num_usdt'] = $sellInfo['num_usdt'];
                                $add['wallet_address'] = $walletArr[$add['type']];
                                $add['phone'] = $phoneArr[$add['type']];
                                $add['createtime'] = time();

                                $res = $m_ordercenter->add($add);
                                if ($res) {
                                    
                                    $this->ajaxReturn(array(
                                        'code'=>1,
                                        'msg' => '操作成功',
                                        'res' => $res,
                                        'data'=> $add
                                    ));
                                }else{
                                    $this->ajaxReturn(array(
                                        'code'=>0,
                                        'msg' => '操作失败'
                                    ));
                                }
                            }
                        }else{
                            $this->ajaxReturn(array(
                                'code'=>0,
                                'msg' => '您有未完成订单，无法继续购买'
                            ));
                        }

                    }

                }else{
                    $this->ajaxReturn(array(
                        'code'=>0,
                        'msg' => '交易大厅订单充足，无法继续购买'
                    ));
                }
            }
        }else{
            $this->redirect('Common/BadRequest');
        }
    }


    /**
     * [showcenter 显示打款凭证]
     * @return [type] [description]
     */
    public function showcenter(){
        $id = $_REQUEST['id'];

        $m_ordercenter = M('Order_center');

        $where_order['id'] = $id;
        $ordercenterInfo = $m_ordercenter->where($where_order)->find();

        
        $this->assign('ordercenterInfo',$ordercenterInfo);
        $this->display();
    }

    /**
     * [ctrl_submitCenterPic 上传凭证]
     * @return [type] [description]
     */
    public function ctrl_submitCenterPic(){
        if (IS_AJAX) {
            $id = $_REQUEST['orderid'];
            $pic_url = trim($_REQUEST['pic_url']);

            $m_ordercenter = M('Order_center');

            $where['id'] = $id;
            $save['pic_url'] = $pic_url;
            $res = $m_ordercenter->where($where)->save($save);
            if ($res) {
                $this->ajaxReturn(array(
                    'code'=>1,
                    'msg' => '提交成功'
                ));
            }else{
                $this->ajaxReturn(array(
                    'code'=>0,
                    'msg' => '提交失败，请重新尝试'
                ));
            }
        }else{
            $this->redirect('Common/BadRequest');
        }
    }


    /**
     * [ctrl_buyCenterFinish 购买完成]
     * @return [type] [description]
     */
    public function ctrl_buyCenterFinish(){
        if (IS_AJAX) {
            $id = $_REQUEST['id'];

            $m_ordercenter = M('Order_center');

            $where['id'] = $id;

            $orderInfo = $m_ordercenter->where($where)->find();
            if ($orderInfo['status'] == 1) {
                $this->ajaxReturn(array(
                    'code'=>1,
                    'msg' => '请勿重复操作'
                ));
            }else{
                $save['status'] = 1;
                $save['paytime'] = time();

                $res = $m_ordercenter->where($where)->save($save);
                if ($res) {
                    // 发送通知短信
                    sendToMobile($orderInfo['phone'],1);
                    $this->ajaxReturn(array(
                        'code'=>1,
                        'msg' => '确认成功，请耐心等待审核'
                    ));
                }else{
                    $this->ajaxReturn(array(
                        'code'=>0,
                        'msg' => '操作失败'
                    ));
                }
            }
        }else{
            $this->redirect('Common/BadRequest');
        }
    }



    /**
     * [buykey 购买秘钥]
     * @return [type] [description]
     */
    public function buykey(){
        $uid = $_SESSION['uid'];

        $lowestTime = strtotime(C('lowestTime'));//开始时间
        $biggestTime = strtotime(C('biggestTime'));//结束时间

        $nowTime = time();

        $m_user = M('User');
        $m_sellkey = M('Sell_key');
        $m_orderkey = M('Order_key');

        if ($nowTime >= $lowestTime && $nowTime < $biggestTime ) {
        
        
            $where_user['id'] = $uid;
            $userInfo = $m_user->where($where_user)->find();

            $sellkeyList = $m_sellkey->order('id asc')->select();

            $where_ordercenter['user_id'] = $uid;
            // $count = $m_orderkey->where($where_ordercenter)->count();
            // $Page  = new \Think\Page($count,20);
            // $show  = $Page->show();

            // $orderkeyList = $m_orderkey->where($where_ordercenter)->order('id desc')->limit($Page->firstRow.','.$Page->listRows)->select();
            $orderkeyList = $m_orderkey->where($where_ordercenter)->order('id desc')->limit(5)->select();

            $this->assign('userInfo',$userInfo);
            $this->assign('sellkeyList',$sellkeyList);
            // $this->assign('page',$show);
            $this->assign('orderkeyList',$orderkeyList);
            $this->display();

        }else{
            $this->redirect('Common/notopen');
        }
        
    }




    /**
     * [ctrl_buyKey 购买平台中心积分]
     * @return [type] [description]
     */
    public function ctrl_buyKey(){
        if (IS_AJAX) {
            $id = $_REQUEST['id'];
            $uid = $_SESSION['uid'];

            $m_sellkey = M('Sell_key');
            $m_orderkey = M('Order_key');
            $m_user = M('User');

            $where_user['id'] = $uid;
            $userStatus = $m_user->where($where_user)->getfield('status');
            if ($userStatus == 0) {
                $this->ajaxReturn(array(
                    'code'=>0,
                    'msg' => '账户未激活，无法购买'
                ));
            }elseif ($userStatus == 2) {
                $this->ajaxReturn(array(
                    'code'=>0,
                    'msg' => '账户已冻结，无法购买'
                ));
            }else{
                $where_sell['id'] = $id;
                $sellInfo = $m_sellkey->where($where_sell)->find();
                if (empty($sellInfo['id'])) {
                    $this->ajaxReturn(array(
                        'code'=>0,
                        'msg' => '购买目标不存在，请刷新后重新操作'
                    ));
                }else{
                    // print_r($sellInfo);die();
                    $where_orderkey['user_id'] = $uid;
                    $where_orderkey['status'] = array('in','0,1');
                    $orderkeyInfo = $m_orderkey->where($where_orderkey)->order('id desc')->find();
                    if (empty($orderkeyInfo['id'])) {
                        $now_time = time();
                        $lastorderInfo = $m_orderkey->order('id desc')->find();

                        if (empty($lastorderInfo['id'])) {
                            $add['type'] = 0;
                            $difference_time = 10;
                        }else{
                            $difference_time = $now_time - $lastorderInfo['createtime'];
                            if ($lastorderInfo['type'] >= 3) {
                                $add['type'] = 0;
                            }else{
                                $add['type'] = $lastorderInfo['type'] + 1;
                            }
                        }

                        if ($difference_time < 5 ) {
                            $this->ajaxReturn(array(
                                'code'=>0,
                                'msg' => '系统繁忙,请稍后购买'
                            ));
                        }else{
                            $walletArr = explode(',', $sellInfo['wallet_address']);
                            $phoneArr = explode(',', $sellInfo['phone']);


                            $add['user_id'] = $uid;
                            $add['num'] = $sellInfo['num'];
                            $add['discount'] = $sellInfo['discount'];
                            $add['num_usdt'] = $sellInfo['num_usdt'];
                            $add['wallet_address'] = $walletArr[$add['type']];
                            $add['phone'] = $phoneArr[$add['type']];
                            $add['createtime'] = time();

                            $res = $m_orderkey->add($add);
                            if ($res) {
                                $this->ajaxReturn(array(
                                    'code'=>1,
                                    'msg' => '操作成功',
                                    'res' => $res,
                                    'data'=> $add
                                ));
                            }else{
                                $this->ajaxReturn(array(
                                    'code'=>0,
                                    'msg' => '操作失败'
                                ));
                            }
                        }
                    }else{
                        $this->ajaxReturn(array(
                            'code'=>0,
                            'msg' => '您有未完成订单，无法继续购买'
                        ));
                    }

                }
            }
        }else{
            $this->redirect('Common/BadRequest');
        }
    }



    /**
     * [showkey 显示打款凭证]
     * @return [type] [description]
     */
    public function showkey(){
        $id = $_REQUEST['id'];

        $m_orderkey = M('Order_key');

        $where_order['id'] = $id;
        $orderkeyInfo = $m_orderkey->where($where_order)->find();

        
        $this->assign('orderkeyInfo',$orderkeyInfo);
        $this->display();
    }



    /**
     * [ctrl_submitKeyPic 上传凭证]
     * @return [type] [description]
     */
    public function ctrl_submitKeyPic(){
        if (IS_AJAX) {
            $id = $_REQUEST['orderid'];
            $pic_url = trim($_REQUEST['pic_url']);

            $m_orderkey = M('Order_key');

            $where['id'] = $id;
            $save['pic_url'] = $pic_url;
            $res = $m_orderkey->where($where)->save($save);
            if ($res) {
                $this->ajaxReturn(array(
                    'code'=>1,
                    'msg' => '提交成功'
                ));
            }else{
                $this->ajaxReturn(array(
                    'code'=>0,
                    'msg' => '提交失败，请重新尝试'
                ));
            }
        }else{
            $this->redirect('Common/BadRequest');
        }
    }



    /**
     * [ctrl_buyKeyFinish 购买完成]
     * @return [type] [description]
     */
    public function ctrl_buyKeyFinish(){
        if (IS_AJAX) {
            $id = $_REQUEST['id'];

            $m_orderkey = M('Order_key');

            $where['id'] = $id;

            $orderInfo = $m_orderkey->where($where)->find();
            if ($orderInfo['status'] == 1) {
                $this->ajaxReturn(array(
                    'code'=>1,
                    'msg' => '请勿重复操作'
                ));
            }else{
                $save['status'] = 1;
                $save['paytime'] = time();

                $res = $m_orderkey->where($where)->save($save);
                if ($res) {
                    // 发送通知短信
                    sendToMobile($orderInfo['phone'],1);
                    $this->ajaxReturn(array(
                        'code'=>1,
                        'msg' => '确认成功，请耐心等待审核'
                    ));
                }else{
                    $this->ajaxReturn(array(
                        'code'=>0,
                        'msg' => '操作失败'
                    ));
                }
            }
        }else{
            $this->redirect('Common/BadRequest');
        }
    }
}